import swal from 'sweetalert2';
import { User } from '../../../../core/auth/user.service';
import { CommonService } from '../../../../core/common/common.service';
import { Component, EventEmitter, OnInit, ViewChild, Input, Output } from '@angular/core';
import { TableDictService } from '../../../../core/common/table-dict.service';
import { SelectComponent } from '../../../../shared/component/ng2-select/select';
import { CustomerService } from '../../service/customer.service';
import { TranslateService } from '@ngx-translate/core';
@Component({
  selector: 'ncloud-customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.scss']
})
export class CustomerComponent implements OnInit {
  @Input() public customer: any = { custType: '0' };
  @Input() public clearCustomer = false;
  public customers: any[] = [];
  @ViewChild('custTypeSelect') custTypeSelect: SelectComponent;
  @ViewChild('custSelect') custSelect: SelectComponent;
  @ViewChild('industry') industry: SelectComponent;
  @ViewChild('companyCodeSelect') companyCodeSelect: SelectComponent;
  @ViewChild('valForm') valForm: any;
  @Output() public cselect: EventEmitter<any> = new EventEmitter();
  constructor(public common: CommonService, public user: User, private tableDict: TableDictService,
    private customerService: CustomerService, private translate: TranslateService) { }
  ngOnInit() {
    this.initCustomer();
    this.initSelect();
  }
  private initCustomer(): void {
    if (!this.clearCustomer) {
      let c = this.customer.custType;
      this.common.clearObject(this.customer);
      if (!c) {
        c = '0';
      }
      this.customer.custType = c;

    }
    if (!this.customer.valForm) {
      this.customer.valForm = this.valForm;
    }
  }
  private initSelect(): void {
    this.tableDict.get('CUST_TYPE').subscribe((data: any) => {
      this.custTypeSelect.items = data;
      this.common.setSelect(this.customer.custType, this.custTypeSelect, true);
      this.custTypeSelected({ id: '0' });
    });
    this.tableDict.get('INDUSTRY').subscribe((data: any) => {
      this.industry.items = data;
    });
  }
  public custTypeSelected(value: any): void {
    this.initCustomer();
    if (value.id === '1') {
      this.customers = [];
      this.customerService.getCustomers(null, null).subscribe((data: any) => {
        const codeSelectData = [];
        data.forEach((c: any) => {
          c.id = c.custId;
          c.text = c.custName;
          const b: any = {};
          b.id = c.custId;
          b.text = c.companyCode;
          codeSelectData.push(b);
        });
        this.customers = data;
        if (this.companyCodeSelect) {
          this.companyCodeSelect.items = codeSelectData;
        }
        if (this.custSelect) {
          this.custSelect.items = data;
        }
        if (this.custSelect) {
          this.common.setSelect(this.customer.custId, this.custSelect, false);
        }
      });
    } else if (value.id === '0') {
      this.customer.modifyTag = '1';
    }
    this.cselect.emit(null);
  }
  public custSelected($event: any): void {
    this.customers.forEach((c: any) => {
      if (c.id === $event.id) {
        this.common.copy(this.customer, c);
        this.customer.modifyTag = '4';
        this.customer.readOnly = true;
        this.customer.custType = '1';
        if (this.companyCodeSelect) {
          if (!this.companyCodeSelect.active || this.companyCodeSelect.active.length === 0 ||
            this.companyCodeSelect.active[0].id !== c.id) {
            this.common.setSelect(c.id, this.companyCodeSelect, false);
          }
        }
      }
    });
    this.cselect.emit($event);
  }
  public companyCodeSelected($event: any): void {
    this.customers.forEach((c: any) => {
      if (c.id === $event.id) {
        this.common.copy(this.customer, c);
        this.customer.modifyTag = '4';
        this.customer.readOnly = true;
        this.customer.custType = '1';
        if (!this.custSelect.active || this.custSelect.active.length === 0 || this.custSelect.active[0].id !== c.id) {
          this.common.setSelect(c.id, this.custSelect, false);
        }
      }
    });
    this.cselect.emit($event);
  }
  public industrySelected($event: any): void {

  }
  public isExistsAccount(): void {
    this.customerService.isExistsAccount(this.customer.contactEmail).subscribe((flag: any) => {
      if (flag) {
        this.translate.get('crm.component.customer.isExistsAccount').subscribe((res: string) => {
          swal({
            text: res,
            showConfirmButton: true,
            timer: 4000,
            type: 'warning'
          }).then((e: any) => {
            this.customer.contactEmail = '';
          }, (dismiss: any) => {
            this.customer.contactEmail = '';
          });
        });
      }
    });
  }
}
